package cn.iocoder.yudao.module.shop.info.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.shop.info.convert.ShopEnterOrderConvert;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopEnterOrderDO;
import cn.iocoder.yudao.module.shop.info.service.ShopEnterOrderService;
import cn.iocoder.yudao.module.shop.info.service.ShopInfoService;
import cn.iocoder.yudao.module.shop.info.vo.ShopEnterOrderPageReqVO;
import cn.iocoder.yudao.module.shop.info.vo.ShopEnterOrderRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 店铺缴费")
@RestController
@RequestMapping("/shop/enter-order")
@Validated
public class ShopEnterOrderController {

    @Resource
    private ShopEnterOrderService shopEnterOrderService;
    @Resource
    private ShopInfoService shopInfoService;

    @GetMapping("/get")
    @Operation(summary = "获得店铺入驻缴费详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:enter-order:query')")
    public CommonResult<ShopEnterOrderRespVO> getEnterOrder(@RequestParam("id") Long id) {
        ShopEnterOrderDO orderDO = shopEnterOrderService.getEnterOrder(id);
        return success(BeanUtils.toBean(orderDO, ShopEnterOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺入驻缴费分页")
    @PreAuthorize("@ss.hasPermission('shop:enter-order:query')")
    public CommonResult<PageResult<ShopEnterOrderRespVO>> getEnterOrderPage(@Valid ShopEnterOrderPageReqVO pageReqVO) {
        PageResult<ShopEnterOrderDO> pageResult = shopEnterOrderService.getEnterOrderPage(pageReqVO);
        Set<Long> shopIds = pageResult.getList().stream().map(ShopEnterOrderDO::getShopId).collect(Collectors.toSet());
        Map<Long, ShopInfoDTO> shops = shopInfoService.getInfoMap(shopIds);
        return success(ShopEnterOrderConvert.INSTANCE.convert(pageResult, shops));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺入驻缴费 Excel")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEnterApplyExcel(@Valid ShopEnterOrderPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopEnterOrderRespVO> list = getEnterOrderPage(pageReqVO).getData().getList();
        // 导出 Excel
        ExcelUtils.write(response, "店铺入驻缴费.xls", "数据", ShopEnterOrderRespVO.class, list);
    }

}
